package com.demo2do.fms.admin.web.controller.supplier.sub.info;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.export.supplier.SupplierSheet;
import com.demo2do.fms.admin.entity.search.supplier.SubSupplierSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.visitor.impl.PersistentVisitor;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 * 分属供方信息查询
 * 业务逻辑：
 * 		查询下属供方的信息(只查下级)
 * 
 * Created by zhonghui on 2017/6/5.
 */
@Controller
@RequestMapping("/sub-suppliers")
@MenuSetting("menu-supplier")
public class SubSupplierInfoController {
    
    @Autowired
    private SupplierService supplierService;

    @Autowired
    @Qualifier("dataService")
    private DataService<Organization> organizationService;
    
    @Autowired
    @Qualifier("persistentVisitor")
    private PersistentVisitor persist;
    /**
     * list 分属供方
     *
     * @param login
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-sub-supplier-management")
    public ModelAndView list(@Secure Login login, HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/supplier/sub-supplier-list");
        
        List<Long> supplierIds = supplierService.listSubSupplierId(login);
        
        int totals = supplierIds.size();
        if(totals > 0) {
            
        	List<Supplier> suppliers = supplierService.listSubSupplier(login, page.with(totals));
            modelAndView.addObject("suppliers", suppliers);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }

    /**
     * search panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/supplier/sub-supplier-search");
        
        modelAndView.addObject("supplier", login.isSupplier());
        
        return modelAndView;
        
    }

    /**
     * on search 公司信息查询
     * 
     * @param login
     * @param search
     * @param parentCode
     * @param parentName
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-sub-supplier-management")
    public ModelAndView onSearch(@Secure Login login, SubSupplierSearch search,
                                 @RequestParam(name = "parentCode", required = false) String parentCode,
                                 @RequestParam(name = "parentName", required = false) String parentName,
                                 HttpServletRequest request, Page page) {

        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/supplier/sub-supplier-list");
        
        if (login.isSupplier()) {
            Supplier parent = supplierService.load(Supplier.class, login.getSupplierId());
        	List<Long> supplierIds = supplierService.listSponsoredSupplierId(parent);
        	search.addSupplierIds(supplierIds);
        } else {
            if (StringUtils.isNotBlank(parentCode) || StringUtils.isNotBlank(parentName)) {
                List<Long> supplierIds = supplierService.listSponsoredSupplierId(parentCode, parentName);
                search.addSupplierIds(supplierIds);
            }
        }

        int totals = supplierService.count(Supplier.class, search.getFilter()); 
        if(totals > 0) {
        	
            List<Supplier> suppliers = supplierService.search(Supplier.class, search, page.with(totals));
            
            modelAndView.addObject("suppliers", suppliers);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView; 
        
    }
    
    /**
     * 导出所属供方
     * 
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("supplier/sub/info/supplier/sub-supplier-export");
        return modelAndView;
    }
    
    /**
     * 导出所属供方
     * 
     * @param search
     * @param login
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public ModelAndView onExport(SubSupplierSearch search, @Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("supplier/supplier-info-export.xlsx"));
        
        if (login.isSupplier()) {
            List<Long> supplierIds = supplierService.listSubSupplierId(login);
        	search.addSupplierIds(supplierIds);
        }
        
        List<SupplierSheet> suppliers = supplierService.export(search);
        modelAndView.addObject("suppliers", suppliers);
        
        modelAndView.addObject("persist", persist);
        
        return modelAndView;
        
    }
    
}